package com.edu.kindergarten.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.edu.kindergarten.R;
import com.edu.kindergarten.server.AccountHelper;
import com.edu.kindergarten.utils.LogUtils;
import com.edu.kindergarten.utils.UiUtils;

public class KinderGartenHomeActivity extends BaseFragmentActivity {
	
	private static final String TAG = "KinderGartenHomeActivity";
	
	private int tabIds[] = new int[]{
			R.id.parent_assistant,
			R.id.teacher_assistant,
			R.id.garten_assistant,
			R.id.personal_center,
			R.id.other,
	};
	
	private RelativeLayout mTopBar;
	
	private ParentHomeFragment mParentHomeTab;
	private TeacherHomeFragment mTeacherHomeTab;
	private GartenHomeFragment mGartenHomeTab;
	private PersonalCenterFragment mPersonalCenterFragment;
	private OtherFragment mOtherFragment;
	
	long lastTime = 0;

	@Override
	public void onAttachFragment(Fragment fragment) {
		// TODO Auto-generated method stub
		super.onAttachFragment(fragment);
		LogUtils.d(TAG,"onAttachFragment");
		
		if (mParentHomeTab == null && fragment instanceof ParentHomeFragment) {
			mParentHomeTab = (ParentHomeFragment)fragment;
		}else if (mTeacherHomeTab == null && fragment instanceof TeacherHomeFragment) {
			mTeacherHomeTab = (TeacherHomeFragment)fragment;
		}else if (mGartenHomeTab == null && fragment instanceof GartenHomeFragment) {
			mGartenHomeTab = (GartenHomeFragment)fragment;
		}else if (mPersonalCenterFragment == null && fragment instanceof PersonalCenterFragment) {
			mPersonalCenterFragment = (PersonalCenterFragment)fragment;
		}else if (mOtherFragment == null && fragment instanceof OtherFragment) {
			mOtherFragment = (OtherFragment)fragment;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LogUtils.d(TAG,"onDestroy");
		//removeAllFragments();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		LogUtils.d(TAG,"onPause");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		LogUtils.d(TAG,"onResume");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		LogUtils.d(TAG,"onStart");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		LogUtils.d(TAG,"onStop");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LogUtils.d(TAG,"onCreate");
		setContentView(R.layout.activity_kinder_garten_home);
		
		mTopBar = (RelativeLayout)findViewById(R.id.top_bar);
		
		//set account name
		TextView nameTextView = (TextView)findViewById(R.id.name);
		nameTextView.setText(AccountHelper.getLastLoginUserName(this));
		
		//set account image
		ImageView userImageView = (ImageView)findViewById(R.id.user_icon);
		userImageView.setImageResource(R.drawable.logo);
		
		RadioGroup tabButtonGroup = (RadioGroup) findViewById(R.id.bottom_bar);
		tabButtonGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				for (int i = 0; i < tabIds.length; i++) {
					if (tabIds[i] == checkedId) {
						setSelection(i);
						break;
					}
				}
			}
		});
		
		setSelection(0);
	}
	
	private void setSelection(int position){
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		hideAllFragments(ft);
		
		switch (position) {
		case 0:
			if (mParentHomeTab == null) {
				mParentHomeTab = new ParentHomeFragment();
				ft.add(R.id.content, mParentHomeTab);
			}else {
				ft.show(mParentHomeTab);
			}
			
			mTopBar.setBackgroundResource(R.drawable.home_parent_bar);
			break;
			
		/*case 1:
		case 2:
		case 3:
		case 4:
			if (mPersonalCenterFragment == null) {
				mPersonalCenterFragment = new PersonalCenterFragment();
				ft.add(R.id.content, mPersonalCenterFragment);
			}else {
				ft.show(mPersonalCenterFragment);
			}
			break;*/
			
		case 1:
			if (mTeacherHomeTab == null) {
				mTeacherHomeTab = new TeacherHomeFragment();
				ft.add(R.id.content, mTeacherHomeTab);
			}else {
				ft.show(mTeacherHomeTab);
			}
			
			mTopBar.setBackgroundResource(R.drawable.home_teacher_bar);
			break;
			
		case 2:
			if (mGartenHomeTab == null) {
				mGartenHomeTab = new GartenHomeFragment();
				ft.add(R.id.content, mGartenHomeTab);
			}else {
				ft.show(mGartenHomeTab);
			}
			
			mTopBar.setBackgroundResource(R.drawable.home_garten_bar);
			break;
			
		case 3:
			if (mPersonalCenterFragment == null) {
				mPersonalCenterFragment = new PersonalCenterFragment();
				ft.add(R.id.content, mPersonalCenterFragment);
			}else {
				ft.show(mPersonalCenterFragment);
			}
			
			mTopBar.setBackgroundResource(R.drawable.home_parent_bar);
			break;
			
		case 4:
			if (mOtherFragment == null) {
				mOtherFragment = new OtherFragment();
				ft.add(R.id.content, mOtherFragment);
			}else {
				ft.show(mOtherFragment);
			}
			
			mTopBar.setBackgroundResource(R.drawable.home_parent_bar);
			break;

		default:
			break;
		}
		
		ft.commit();
	}
	
	private void hideAllFragments(FragmentTransaction ft){
		if (mParentHomeTab != null) {
			ft.hide(mParentHomeTab);
		}
		
		if (mTeacherHomeTab != null) {
			ft.hide(mTeacherHomeTab);
		}
		
		if (mGartenHomeTab != null) {
			ft.hide(mGartenHomeTab);
		}
		
		if (mPersonalCenterFragment != null) {
			ft.hide(mPersonalCenterFragment);
		}
		
		if (mOtherFragment != null) {
			ft.hide(mOtherFragment);
		}
	}
	
	private void removeAllFragments(){
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		
		if (mParentHomeTab != null) {
			ft.remove(mParentHomeTab);
			mParentHomeTab = null;
		}
		
		if (mTeacherHomeTab != null) {
			ft.remove(mTeacherHomeTab);
			mTeacherHomeTab = null;
		}
		
		if (mGartenHomeTab != null) {
			ft.remove(mGartenHomeTab);
			mGartenHomeTab = null;
		}
		
		if (mPersonalCenterFragment != null) {
			ft.remove(mPersonalCenterFragment);
			mPersonalCenterFragment = null;
		}
		
		if (mOtherFragment != null) {
			ft.remove(mOtherFragment);
			mOtherFragment = null;
		}
		
		ft.commit();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		long curTime = System.currentTimeMillis();
		LogUtils.d(TAG,"onBackPressed,lastTime:" + lastTime + 
				",curTime:" + curTime +
				",curTime - lastTime" + (curTime - lastTime));
		if (lastTime == 0) {
			UiUtils.showShortToast(this, R.string.press_again_quit_app);
			lastTime = curTime;
			return;
		}else if (lastTime > 0 && curTime - lastTime > 500) {
			if (curTime - lastTime > 3000) {
				lastTime = 0;
				return;
			}else {
				finish();
			}
		}
	}
}
